package com.cosina.game.crystallight.ui.battle.drawable;

import java.util.Iterator;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;

import com.cosina.game.crystallight.engine.BattleEngine;
import com.cosina.game.crystallight.engine.EnemyManager;
import com.cosina.game.crystallight.engine.EvilNest;
import com.cosina.game.crystallight.model.enemy.Enemy;
import com.cosina.game.crystallight.ui.battle.BattleResource;
import com.cosina.game.crystallight.ui.battle.drawadapter.BaseDrawable;
import com.cosina.game.crystallight.ui.common.PaintSuite;
import com.cosina.game.crystallight.ui.monster.MonsterFrames;
import com.cosina.game.crystallight.ui.monster.MonsterResource;

public class EnemyAndPanelDrawable extends BaseDrawable {
	private Bitmap wavePanel;
	private EnemyManager enemyManager;
	private final Point enemyHomeLocation;
	private final Point wavePanelLocation;
	private final Point waveCommingLocation;

	int wavePanelHeight = 0;

	public EnemyAndPanelDrawable() {
		int wave = BattleEngine.tollGate.getWaveCount();
		this.wavePanel = BattleResource.createWavePanelImage(wave);
		this.wavePanelHeight = wavePanel.getHeight();
		this.enemyManager = BattleEngine.enemyManager;

		Point enemyHomeLocationTile = BattleEngine.field.locationSource();
		enemyHomeLocation = new Point(enemyHomeLocationTile.x * 44 + 5
				+ enemyHomeXSpan, enemyHomeLocationTile.y * 44 + 5
				+ enemyHomeYSpan);

		wavePanelLocation = new Point(8 + 5,
				(enemyHomeLocationTile.y + 2) * 44 + 6 + 5);
		waveCommingLocation = new Point(35 + 5,
				(enemyHomeLocationTile.y + 2) * 44 + 15);
	}

	@Override
	public void draw(Canvas canvas) {
		{
			Iterator<Enemy> iter = enemyManager.enemies.iterator();
			while (iter.hasNext()) {
				Enemy enemy = iter.next();
				if (enemy.staus == Enemy.STATUS_RUNNING) {
					MonsterFrames mf = MonsterResource.monsterBitmaps[enemy.kind];
					canvas.drawBitmap(mf.getFrame(enemy.getDirection(), enemy
							.getFrameIndex()), enemy.center.x - 22 + mf.xspan
							+ 5, enemy.center.y - 22 + mf.yspan + 5,
							PaintSuite.p4Simple);

					if (enemy.damage()) {
						canvas.drawRect(enemy.center.x - 22 + mf.xspan + 5,
								enemy.center.y - 22 + mf.yspan + 5,
								enemy.center.x - 22 + mf.xspan + 5 + 44
										* enemy.getHpPercent(), enemy.center.y
										- 22 + mf.yspan + 5 + 2,
								PaintSuite.p4HpLine);
					}
				}
			}
		}

		{
			canvas.drawBitmap(BattleResource.enemyHome, enemyHomeLocation.x,
					enemyHomeLocation.y, PaintSuite.p4Simple);
			EvilNest evilNest = BattleEngine.wavePanelManager;
			if (evilNest.hasNextLevel()) {
				if (evilNest.index != 0) {
					Rect src = new Rect(0, 38 * evilNest.currentWave, 25, 38
							* evilNest.currentWave + evilNest.index);
					int heightSpan = src.bottom > wavePanelHeight ? src.bottom
							- wavePanelHeight : 0;
					Rect to = new Rect(wavePanelLocation.x, wavePanelLocation.y
							- evilNest.index,
							wavePanelLocation.x + src.width(),
							wavePanelLocation.y - heightSpan);
					canvas.drawBitmap(wavePanel, src, to, PaintSuite.p4Simple);
				}

				if (evilNest.knocking == false) {
					String str = String.valueOf(evilNest.score).split("\\.")[0];
					char[] diagtals = str.toCharArray();
					int diagtalsCount = diagtals.length;
					for (int i = diagtalsCount - 1; i >= 0; i--) {
						for (int j = 0; j < 10; j++) {
							if (foo[j] == diagtals[i]) {
								canvas.drawBitmap(BattleResource.digital[j],
										waveCommingLocation.x
												- (diagtalsCount - i) * 7,
										waveCommingLocation.y,
										PaintSuite.p4Simple);
							}
						}
					}
					// canvas.drawText(str,
					// waveCommingLocation.x-PaintSuite.p4SmallDight.measureText(str),
					// waveCommingLocation.y, PaintSuite.p4SmallDight);
				}
			}
		}
	}

	private char[] foo = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

	final static int enemyHomeYSpan = 6, enemyHomeXSpan = 0;
}
